﻿using System;
using System.Web;
using System.Web.UI;
using System.Text;
namespace IPMS.Common
{
    /// <summary>
    /// </summary>


    public partial class Jscript
    {        
               
        /// <summary>
        /// JavaScript pop-up small window
        /// </summary>
        /// <param name="js"> window's Information</param>
        public static void Alert(string message)
        {
            #region
            string js = @"<Script language='JavaScript'>
                    alert('" + message + "');</Script>";
            HttpContext.Current.Response.Write(js);
            #endregion
        }

        /// <summary>
        /// JavaScript pop-up small window
        /// </summary>
        /// <param name="js"> window's Information</param>
        public static void Alert(string message, Page page)
        {
            #region
            string js = @"<Script language='JavaScript'>
                    alert('" + message + "');</Script>";
            page.RegisterStartupScript("alert", js);
            #endregion
        }

        /// <summary>
        /// Pop-up message box and turning to a new URL
        /// </summary>
        /// <param name="message">News content</param>
        /// <param name="toURL">Connect Address</param>
        public static void AlertAndRedirect(string message, string toURL,Page page)
        {
            //#region
            //string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
            //HttpContext.Current.Response.Write(string.Format(js, message, toURL));
            //HttpContext.Current.Response.End();
            //#endregion

            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", message);
            builder.AppendFormat("top.location.href='{0}'", toURL);
            builder.Append("</script>");
            page.RegisterStartupScript("alert", builder.ToString());

        }

        /// <summary>
        /// JavaScript pop-up small window in ajax
        /// </summary>
        /// <param name="strMessage"></param>
        /// <param name="up"></param>
        public static void AlertInAjax(string strMessage, UpdatePanel up)
        {
            ScriptManager.RegisterClientScriptBlock(up, up.GetType(), "", "alert('" + strMessage + "')", true);
        }

        /// <summary>
        /// JavaScript pop-up small window in ajax
        /// </summary>
        /// <param name="strMessage"></param>
        /// <param name="up"></param>
        public static void AlertAndRedirectInAjax(string strMessage, string toURL, UpdatePanel up)
        {
            ScriptManager.RegisterClientScriptBlock(up, up.GetType(), "", "alert('" + strMessage + "');window.location.replace('" + toURL + "')",  true);
        }
        
        /// <summary>
        /// Back to the history of the page
        /// </summary>
        /// <param name="value">-1/1</param>
        public static void GoHistory(int value)
        {
            #region
            string js = @"<Script language='JavaScript'>
                    history.go({0});  
                  </Script>";
            HttpContext.Current.Response.Write(string.Format(js, value));
            HttpContext.Current.Response.End();
            #endregion
        }

        /// <summary>
        /// Close the current window
        /// </summary>
        public static void CloseWindow()
        {
            #region
            string js = @"<Script language='JavaScript'>
                    parent.opener=null;window.close();  
                  </Script>";
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
            #endregion
        }

        /// <summary>
        /// Refresh parent window
        /// </summary>
        public static void RefreshParent(string url)
        {
            #region
            string js = @"<script>try{top.location="""+url+@"""}catch(e){location="""+url+@"""}</script>";
            HttpContext.Current.Response.Write(js);
            #endregion
        }

       
        /// <summary>
        /// Open the window refresh
        /// </summary>
        public static void RefreshOpener()
        {
            #region
            string js = @"<Script language='JavaScript'>
                    opener.location.reload();
                  </Script>";
            HttpContext.Current.Response.Write(js);
            #endregion
        }


        /// <summary>
        /// Open a new form of specified sizes
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="width">width</param>
        /// <param name="heigth">heigth</param>
        /// <param name="top">top</param>
        /// <param name="left">left</param>
        public static void OpenWebFormSize(string url, int width,int heigth,int top,int left)
        {
            #region
            string js = @"<Script language='JavaScript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top="+top+",left="+left+",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>";

            HttpContext.Current.Response.Write(js);
            #endregion
        }
       
        
        /// <summary>
        /// Turn the page Url developed
        /// </summary>
        /// <param name="url">url</param>
        public static void JavaScriptLocationHref(string url)
        {
            #region
            string js = @"<Script language='JavaScript'>
                    window.location.replace('{0}');
                  </Script>";
            js = string.Format(js, url);
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
            #endregion
        }

        /// <summary>
        /// The size of the location of the designated open dialog mode
        /// </summary>
       
        public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
        {
            #region
            string features = "dialogWidth:" + width.ToString() + "px"
                + ";dialogHeight:" + height.ToString() + "px"
                + ";dialogLeft:" + left.ToString() + "px"
                + ";dialogTop:" + top.ToString() + "px"
                + ";center:yes;help=no;resizable:no;status:no;scroll=yes";
            ShowModalDialogWindow(webFormUrl, features);
            #endregion
        }

        public static void ShowModalDialogWindow(string webFormUrl, string features)
        {
            string js = ShowModalDialogJavascript(webFormUrl, features);
            HttpContext.Current.Response.Write(js);
        }

        public static string ShowModalDialogJavascript(string webFormUrl, string features)
        {
            #region
            string js = @"<script language=javascript>							
							showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
            return js;
            #endregion
        } 
       

    }
}
